package io.renren.modules.communityService.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import io.renren.common.validator.Assert;
import io.renren.modules.communityService.dto.MiniAppDbDailyHealthCardInfoDTO;
import io.renren.modules.communityService.dto.MiniAppDbDailyHealthCardSaveDTO;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.communityService.dao.DbDailyHealthCardDao;
import io.renren.modules.communityService.entity.DbDailyHealthCardEntity;
import io.renren.modules.communityService.dto.DbDailyHealthCardDTO;
import io.renren.modules.communityService.vo.DbDailyHealthCardVO;
import io.renren.modules.communityService.service.DbDailyHealthCardService;
import io.renren.modules.communityService.conver.DbDailyHealthCardConver;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;


@Service("dbDailyHealthCardService")
public class DbDailyHealthCardServiceImpl extends ServiceImpl<DbDailyHealthCardDao, DbDailyHealthCardEntity> implements DbDailyHealthCardService {

    @Override
    public PageUtils<DbDailyHealthCardVO> queryPage(DbDailyHealthCardDTO dbDailyHealthCard) {
        IPage<DbDailyHealthCardEntity> page = baseMapper.selectPage(
                new Query<DbDailyHealthCardEntity>(dbDailyHealthCard).getPage(),
                new QueryWrapper<DbDailyHealthCardEntity>()
        );

        return PageUtils.<DbDailyHealthCardVO>page(page).setList(DbDailyHealthCardConver.MAPPER.conver(page.getRecords()));
    }
    @Override
    public DbDailyHealthCardVO getById(Integer id) {
        return DbDailyHealthCardConver.MAPPER.conver(baseMapper.selectById(id));
    }

    @Override
    public boolean save(DbDailyHealthCardDTO dbDailyHealthCard) {
        DbDailyHealthCardEntity dbDailyHealthCardEntity = DbDailyHealthCardConver.MAPPER.converDTO(dbDailyHealthCard);
        return this.save(dbDailyHealthCardEntity);
    }

    @Override
    public boolean updateById(DbDailyHealthCardDTO dbDailyHealthCard) {
        DbDailyHealthCardEntity dbDailyHealthCardEntity = DbDailyHealthCardConver.MAPPER.converDTO(dbDailyHealthCard);
        return this.updateById(dbDailyHealthCardEntity);
    }

    @Override
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }

    @Override
    public boolean removeByIds(Collection<? extends Serializable> ids) {
        return super.removeByIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(MiniAppDbDailyHealthCardSaveDTO dto) {
        DbDailyHealthCardEntity info = this.info(new MiniAppDbDailyHealthCardInfoDTO().setUserId(dto.getUserId()));
        Assert.isTrue(ObjectUtil.isNotNull(info),"今日已健康打卡了");
        DbDailyHealthCardEntity dbDailyHealthCardEntity = DbDailyHealthCardConver.MAPPER.conver1(dto);
        dbDailyHealthCardEntity.setCardDate(DateUtil.formatDate(new Date()));
        dbDailyHealthCardEntity.setCreatedAt(new Date());
        dbDailyHealthCardEntity.setUpdatedAt(new Date());
        this.save(dbDailyHealthCardEntity);
    }

    @Override
    public DbDailyHealthCardEntity info(MiniAppDbDailyHealthCardInfoDTO dto) {
        DbDailyHealthCardEntity one = this.getOne(new QueryWrapper<DbDailyHealthCardEntity>().lambda()
                .eq(DbDailyHealthCardEntity::getUserId,dto.getUserId())
                .eq(DbDailyHealthCardEntity::getCardDate,DateUtil.formatDate(new Date()))
        );
        return one;
    }

}
